/*
 * Copyright © Yan Zhenjie. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ly.hotel.httpserver.response;

import android.util.Log;

import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SysConf;
import com.yanzhenjie.andserver.RequestHandler;
import com.yanzhenjie.andserver.util.HttpRequestParser;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <p>Login Handler.</p>
 * Created by Yan Zhenjie on 2016/6/13.
 */
public class RequestLoginHandler implements RequestHandler {
	private static HashMap<String, String> cmd2desc = new HashMap<String, String>();
	private static String userName = "";
	private static String password = "";
	
    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
        Map<String, String> params = HttpRequestParser.parse(request);
        
        Log.i("AndServer", "Params: " + params.toString());
        
        if(userName.equals("") || password.equals("")){
	        userName = params.get("username");
	        password = params.get("password");
        }

        if ("pre".equals(userName) && "6111".equals(password)) {
            StringEntity stringEntity = new StringEntity(getSpaInfo(), "GBK");
            stringEntity.setContentType("text/html");
            response.setEntity(stringEntity);
        } else {
            StringEntity stringEntity = new StringEntity("Login Failed", "GBK");
            response.setEntity(stringEntity);
        }
    }
    
    public String getSpaInfo(){
    	
    	StringBuilder sb = new StringBuilder(); 
    	sb.append("<!DOCTYPE html>\n");
    	sb.append("<html>\n");
    	sb.append("<head>\n");
    	sb.append("<title>SPA信息</title>\n");
    	sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\" />\n");
    	sb.append("<style type=\"text/css\">\n"); 
    	sb.append("</style></head>\n");
    	sb.append("<body bgcolor=\"#FFF8DC\">\n");
    	sb.append("服务器: " + MyApplication.getHostIpAndPort());
    	sb.append("<br/>");
    	sb.append("本机MAC: " + MyApplication.getMAC());
    	sb.append("<br/>");
    	if(MyApplication.shareUid == null || !MyApplication.shareUid.equals("android.uid.system")){
    		sb.append("版本: " + "v" + MyApplication.getApkVersion() + ".noroot" + "(" + MyApplication.getSystemVersion() + 
    				",v" + Constants.LYAGENT_VERSION + ")");
    	} else {
    		sb.append("版本: " + "v" + MyApplication.getApkVersion() + "(" + MyApplication.getSystemVersion() + 
    				",v" + Constants.LYAGENT_VERSION + ")");
    	}
    	sb.append("<br/>");
    	sb.append("<div>\n");
    	sb.append("<br/>");
    	sb.append("<br/>\n"); 
    	
    	Set<String> set = SysConf.extendConfMap.keySet();
    	
    	for(String s : set){
    		sb.append(s);
    		sb.append("[<font color='#00FF00'>" + getConfMean(s) + "</font>]" + " -> " + "<font color='#FF0000'>" + SysConf.extendConfMap.get(s) + "</font>");
    		sb.append("<br/>");
    	}
    	
    	sb.append("<br/><br/>"); 
    	sb.append("</div></body></html>"); 
    	return sb.toString();
    }
    
    private String getConfMean(String conf){
    	if(cmd2desc.isEmpty()){
    		cmd2desc.put("user-login-dialog", "开机刷卡登录");
    		cmd2desc.put("shuaka-show-time", "刷卡提示窗显示时间");
    		cmd2desc.put("shoupai-enable", "使用刷卡功能");
    		cmd2desc.put("me-enable", "小圆点显示'我'按钮");
    		cmd2desc.put("rtmsg-interval", "推送消息获取时间间隔");
    		cmd2desc.put("cloud-ad-enable", "云广告");
    		cmd2desc.put("webappinf-enable", "手机遥控");
    		cmd2desc.put("bl-enable", "背光控制");
    		cmd2desc.put("cmd-interval", "推送命令获取时间间隔");
    		cmd2desc.put("call-sync-interval", "呼叫同步时间间隔");
    		cmd2desc.put("exp-monitor-app-conf", "大屏扩展功能配置");
    		cmd2desc.put("http-proxy", "代理服务器");
    		cmd2desc.put("logo-enable", "是否显示左上logo");
    		cmd2desc.put("cloud-url", "云端服务器地址");
    		cmd2desc.put("interface-mode", "接口调试模式");
    		cmd2desc.put("exp-monitor-app", "大屏扩展app名称");
    		cmd2desc.put("iptv-packet-name", "第三方iptv的包名");
    		cmd2desc.put("factory-debug", "调试模式");
    		cmd2desc.put("cloud_apk_update-enable", "通过云端自动升级最新SPA");
    	}
    	
    	return cmd2desc.get(conf);
    }
}


